package com.gouwudai.gobbs.dao.impl;

import java.math.BigDecimal;
import java.util.List;

import org.apache.log4j.Logger;

import com.gouwudai.gobbs.common.IPageList;
import com.gouwudai.gobbs.dao.ITopicReDAO;
import com.gouwudai.gobbs.pojo.TbbsTopicContent;
import com.gouwudai.gobbs.pojo.TbbsTopicRe;
import com.litt.core.exception.DBException;

public class TopicReDAOImpl extends BaseDAOImpl implements ITopicReDAO 
{
	private static final Logger logger = Logger.getLogger(TopicReDAOImpl.class);	
	private static final String BUSI_NAME = "主题回复信息";
	
	/**
	 * 保存主题回复：先保存回复、再保存内容
	 * @param tbbsTopicRe
	 * @throws DBException
	 */
	public void save(TbbsTopicRe tbbsTopicRe)
	{ 	
		super.saveObject(tbbsTopicRe);		
	}

	/**
	 * 结帖给分
	 * @param topicReId
	 * @param userMark
	 * @throws DBException
	 */
	public void doGiveMark(Integer topicReId,BigDecimal userMark)
	{
		StringBuilder hql = new StringBuilder("update TbbsTopicRe set getMark=? where topicReId=?");
        super.updateObject(hql.toString(), new Object[]{userMark,topicReId});
    }	
	
	public TbbsTopicRe load(Integer id)
	{
		return (TbbsTopicRe)super.loadObject(TbbsTopicRe.class, id);
	} 
	
	/**
     * 逻辑删除回复
     * @param topicReIds
     * @throws DBException
     */
    public void deleteLogic(String topicReIds)
    {
    	StringBuilder hql = new StringBuilder("update TbbsTopicRe set reIsDel=1 where topicReId in(");
        hql.append(topicReIds);
        hql.append(")");
        super.executeHql(hql.toString());
    }
    
	/**
     * 逻辑删除单条回复
     * @param topicReIds
     * @throws DBException
     */
    public void deleteLogic(int topicReId)
    {    	
    	String hql = "update TbbsTopicRe set reIsDel=1 where topicReId="+topicReId;
        super.executeHql(hql);
    }    
    
    /**
     * 还原已删除回复
     * @param topicReIds
     * @throws DBException
     */
    public void doResume(String topicReIds)
    {
    	StringBuilder hql = new StringBuilder("update TbbsTopicRe set reIsDel=0 where topicReId in(");
        hql.append(topicReIds);
        hql.append(")");
        super.executeHql(hql.toString());
    }    
    
    /**
     * 物理删除主题
     * @param topicReId
     * @throws DBException
     */
    public void delete(Integer topicReId)
    {
    	//删除主题回复
    	super.deleteObject(TbbsTopicRe.class.getName(),"topicReId",topicReId);
    	//删除主题回复内容
    	super.deleteObject(TbbsTopicContent.class.getName(),"topicReId",topicReId);
    }
    
/*************************************************************************/    
    /**
     * 查询当前主题的回复信息
     * @param topicId
     * @param pageIndex
     * @param pageSize
     * @return
     * @throws DBException
     */
    public List listByTopic(Integer topicId,int pageIndex,int pageSize)
    {
    	StringBuilder listHql = new StringBuilder("from TbbsTopicRe where topicId=");
    	listHql.append(topicId); 
    	listHql.append(" order by topicReId asc, reOrder desc");

    	return super.listByHql(listHql.toString(),pageIndex,pageSize);
    }
    /**
     * 查询当前主题的回复信息数量（供分页）
     * @param topicId
     * @param pageIndex
     * @param pageSize
     * @return
     * @throws DBException
     */
    public int listCountByTopic(Integer topicId)
    {
    	StringBuilder listHql = new StringBuilder("select count(*) from TbbsTopicRe where topicId=");
    	listHql.append(topicId);
    	//listHql.append(" order by reAddTime desc,topicOrder desc");
    	return super.listHqlCount(listHql.toString());
    }    
/*************************************************************************/
    
    /**
     * 统计用户对该主题的回复数量
     * 使用过的地方：“回复可见”
     * @param topicId
     * @param userId
     */
    public int listCountByReUser(Integer topicId,Integer userId)
    {
    	StringBuilder listHql = new StringBuilder("select count(*) from TbbsTopicRe where topicId=");
    	listHql.append(topicId);
    	listHql.append(" and userId=");
    	listHql.append(userId);
    	//listHql.append(" order by reAddTime desc,topicOrder desc");
    	return super.listHqlCount(listHql.toString());
    }  
    
    /**
     * 查询获得积分的用户
     * @param topicId
     * @return
     * @throws DBException
     */
    public List listGetMark(Integer topicId)
    {
    	StringBuilder listHql = new StringBuilder("select userId,loginId, getMark from TbbsTopicRe where topicId=");
    	listHql.append(topicId);
    	listHql.append(" and getMark is not null and getMark>0  order by reAddTime desc,reOrder desc");
    	return super.listAll(listHql.toString());
    }    
    
    public IPageList listPage(int pageIndex,int pageSize)
    {       
    	String listSql = "SELECT * FROM TbbsTopicRe";    	
  	
    	return super.listSqlPage(listSql.toString(),pageIndex,pageSize);
    } 

    
}
